package com.whmskj.xjlhsc.mobie.equipmentManage.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import com.github.pagehelper.PageHelper;
import com.whmskj.xjlhbc.core.Page;
import com.whmskj.xjlhbc.db.DataSource;
import com.whmskj.xjlhbc.db.DataSourceType;
import com.whmskj.xjlhbc.domain.DictionaryProcess;
import com.whmskj.xjlhsc.controller.vo.Result;
import com.whmskj.xjlhsc.entity.Sb02EquipmentCounterAccount;
import com.whmskj.xjlhsc.entity.Stopreasondeclaration;
import com.whmskj.xjlhsc.mobie.equipmentManage.entity.dto.AddDeclarationDto;
import com.whmskj.xjlhsc.mobie.equipmentManage.entity.vo.JtTcPh;
import com.whmskj.xjlhsc.mobie.equipmentManage.entity.vo.SbStopTimeVo;
import com.whmskj.xjlhsc.mobie.equipmentManage.service.DictionaryProcessService;
import com.whmskj.xjlhsc.service.Sb02EquipmentCounterAccountService;
import com.whmskj.xjlhsc.service.ShachangSbStoptimeService;
import com.whmskj.xjlhsc.service.StopreasondeclarationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 张
 * @version 1.0
 */

@RestController
@RequestMapping("/mob/sbStopRecord")
@Slf4j
@RequiredArgsConstructor
@Tag(description = "停车记录", name = "停车记录")
public class StopRecordController {
    @Autowired
    private ShachangSbStoptimeService shachangSbStoptimeService;
    @Autowired
    private DictionaryProcessService dictionaryProcessService;
    @Autowired
    private StopreasondeclarationService stopreasondeclarationService;
    @Autowired
    private Sb02EquipmentCounterAccountService sb02EquipmentCounterAccountService;

    @GetMapping("/getPCMonth")
    @Operation(description = "获取盘存月")
    public Result getPCMonth() {
        List<Map<String, Object>> list = shachangSbStoptimeService.getPCMonth();
        return Result.success(list);
    }

    @GetMapping("/stopList")
    @Operation(description = "停车记录")
    public Result stopList(@RequestParam String process,@RequestParam(required = false)String keyWord ) {
        List<SbStopTimeVo> list = shachangSbStoptimeService.stopList(process,keyWord);
        return Result.success(list);
    }

    @GetMapping("runStopCar")
    @Operation(description = "运行停车分析表")
    public Result runStopCar(@RequestParam String process,
                             @RequestParam String startTime,
                             @RequestParam String endTime) {
        List<Object> list = shachangSbStoptimeService.runStopCar(process,startTime,endTime);
     return Result.success(list);
    }

    @GetMapping("bantctj")
    @Operation(description = "班次停车统计")
    public Result bantcfx(@RequestParam String process,
                      @RequestParam String startTime,
                      @RequestParam String endTime) {
        List<Map<String, Object>> list = shachangSbStoptimeService.bantcfx(process,startTime,endTime);
        return Result.success(list);
    }

    @GetMapping("banzutctj")
    @Operation(description = "班组停车统计")
    public Result banzutctj(@RequestParam String process,
                          @RequestParam String startTime,
                          @RequestParam String endTime) {
        List<Map<String, Object>> list = shachangSbStoptimeService.banzutctj(process,startTime,endTime);
        return Result.success(list);
    }

    @GetMapping("jttcph")
    @Operation(description = "机台停车排行")
    public Result jttcph(@RequestParam String process,
                      @RequestParam String time,
                         @RequestParam(required = false) String type
                      ) {
        List<JtTcPh> list = shachangSbStoptimeService.jttcph(process,time,type);
        return Result.success(list);
    }

    @PostMapping("addDeclaration")
    @Operation(description = "添加申报")
    public Result addDeclaration(@RequestBody AddDeclarationDto addDeclarationDto) {
        List<Map<String, String>> list = addDeclarationDto.getList();
        List<Stopreasondeclaration> addList = new ArrayList<>();
        for (Map<String, String> map : list) {
            Stopreasondeclaration stopreasondeclaration = new Stopreasondeclaration();

            BeanUtil.copyProperties(addDeclarationDto, stopreasondeclaration);
            stopreasondeclaration.setCreateTime(DateTime.now());
            long millisDiff = addDeclarationDto.getEndTime().getTime() - addDeclarationDto.getStartTime().getTime();
            stopreasondeclaration.setHourTime( millisDiff / 3600000.0+"");
            String plantform = map.get("plantform");
            stopreasondeclaration.setPlantform(plantform);
            String process = map.get("process");
            stopreasondeclaration.setProcess(process);
            String deviceName = map.get("deviceName");
            stopreasondeclaration.setDeviceName(deviceName);
            String mesid = map.get("mesid");
            stopreasondeclaration.setMesId(Integer.parseInt(mesid));
            addList.add(stopreasondeclaration);
        }
        boolean save = stopreasondeclarationService.saveBatch(addList);
        return save?Result.success():Result.error("添加失败");
    }

    @GetMapping("stopDeclarationList")
    @Operation(description = "停车申报列表")
    public Result stopDeclarationList(@RequestParam(required = false) String process,
                                      @RequestParam(required = false) String startTime,
                                      @RequestParam(required = false) String endTime,
                                      @RequestParam(required = false) Integer isEnd) {
        Page.startPage();
        List<Stopreasondeclaration> list = stopreasondeclarationService.stopDeclarationList(process,startTime,endTime,isEnd);
        return Result.success(list);
    }

    @GetMapping("getStopCarRecordById")
    @Operation(description = "根据id获取停车申报")
    public Result getStopCarRecordById(@RequestParam Long id) {
        Stopreasondeclaration stopreasondeclaration = stopreasondeclarationService.getById(id);
        return Result.success(stopreasondeclaration);
    }

    @DeleteMapping("/removeStopCarRecordByid")
    @Operation(description = "删除停车申报")
    public Result removeStopCarRecordByid(@RequestParam Long id) {
        boolean remove = stopreasondeclarationService.removeById(id);
        return remove?Result.success():Result.error("删除失败");
    }

    @PostMapping("updateStopCarRecord")
    @Operation(description = "修改停车申报")
    public Result updateStopCarRecord(@RequestBody Stopreasondeclaration stopreasondeclaration) {
        boolean update = stopreasondeclarationService.updateById(stopreasondeclaration);
        return update?Result.success():Result.error("修改失败");
    }

    @GetMapping("getStopCarReason")
    @Operation(description = "获取停车申报原因")
    public Result getStopCarReason(@RequestParam(required = false)String reason) {
        List<DictionaryProcess> list = dictionaryProcessService.getStopCarReason(reason);
        return Result.success(list);
    }

    @GetMapping("machineGroup")
    @Operation(description = "获取机台分组")
    public Result machineGroup(@RequestParam String process) {
        List<List> list = sb02EquipmentCounterAccountService.machineGroup(process);
        return Result.success(list);
    }

}
